package swu.edu.cn.javaee.service;


import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.component.Image;
import swu.edu.cn.javaee.entity.Photo;
import swu.edu.cn.javaee.entity.PhotoInfo;
import swu.edu.cn.javaee.mapper.PhotoMapper;

import java.util.List;

import static swu.edu.cn.javaee.component.Signal.*;

@Service
public class PhotoService {

    @Resource
    private PhotoMapper photoMapper;

    public String savePhoto(Photo photo){
        String filepath= Image.GenerateImage(photo.getPhoto());
        photo.setPhoto(filepath);
        photoMapper.savePhoto(photo);
        if (photo.getPaCid()!=null){
            return SUCCESS_CODE;
        }else {
            return FAILED_CODE;
        }
    }

    public String updatePhoto(Photo photo){
        Photo photo1=photoMapper.queryById(photo.getPaId());
        if(photo1!=null){
            photoMapper.updatePhoto(photo);
            return SUCCESS_CODE;
        }else {
            return FAILED_CODE_NO_PHOTO;
        }
    }

    public PhotoInfo queryPhoto(String paCid){
        List<Photo> list=photoMapper.queryPhoto(paCid);
        PhotoInfo photoInfo=new PhotoInfo();
        if(list.size()!=0){
            photoInfo.setSignal(SUCCESS_CODE);
            photoInfo.setData(list);
        }else {
            photoInfo.setSignal(FAILED_CODE_NO_PHOTO);
        }
        System.out.println(photoInfo);
        return photoInfo;
    }
    public String deletePhoto(String paId){
        Photo photo=photoMapper.queryById(paId);
        if(photo!=null){
            photoMapper.deletePhoto(paId);
            return SUCCESS_CODE;
        }else {
            return FAILED_CODE_NO_PHOTO;
        }
    }
}
